package com.liang.blog.sensitive_word;


import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordReplace;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;

import java.util.Arrays;
import java.util.List;

public class CustomWordReplace implements IWordReplace {

    @Override
    public void replace(StringBuilder stringBuilder, final char[] rawChars, IWordResult wordResult, IWordContext wordContext) {
        String sensitiveWord = InnerWordCharUtils.getString(rawChars, wordResult);

        // 自定义不同的敏感词替换策略，可以从数据库读取
//        if ("五星红旗".equals(sensitiveWord)) {
//            stringBuilder.append("国家旗帜");
//        } else if ("毛主席".equals(sensitiveWord)) {
//            stringBuilder.append("教员");
//        } else {
            // 其他默认使用 * 代替
            int wordLength = wordResult.endIndex() - wordResult.startIndex();
            stringBuilder.append("*".repeat(Math.max(0, wordLength)));
        }
    }




